package com.kefu.call.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kefu.call.model.RecordLog;
import com.kefu.call.service.RecordLogService;
import com.kefu.common.mybatis.Order;
import com.kefu.sys.model.SysTenant;
import com.kefu.sys.service.SysTenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.common.annotation.Permission;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.DeleteVo;
import com.kefu.common.vo.DetailVo;
import com.kefu.call.model.CdrCallhistory;
import com.kefu.call.service.CdrCallhistoryService;
import com.kefu.call.vo.CdrCallhistorySearchVo;
import com.kefu.call.vo.CdrCallhistoryInsertVo;
import com.kefu.call.vo.CdrCallhistoryUpdateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.util.Optional;

@Api(tags = "计费_话费扣除历史")
@RestController
@RequestMapping("call/cdr_callhistory")
public class CdrCallhistoryController extends AbstractBaseController {

    @Autowired
    private CdrCallhistoryService cdrCallhistoryService;
    @Autowired
    SysTenantService sysTenantService;
    @Autowired
    RecordLogService recordLogService;


    @PostMapping("/list")
    @ApiOperation(value = "查询计费_话费扣除历史", notes = "查询计费_话费扣除历史")
    public Result<Page<CdrCallhistory>> list(@Validated @RequestBody CdrCallhistorySearchVo search) {

        search.setOrderField("oid");
        search.setOrder(Order.DESC);
        Page<CdrCallhistory> page = cdrCallhistoryService.searchLambdaPage(search);
        page.getRecords().forEach(item->{
            if(StrUtil.isNotBlank(item.getCallid())){
                List<RecordLog> recordLogList = recordLogService.list(Wrappers.lambdaQuery(RecordLog.class)
                        .orderByAsc(RecordLog::getStartTime).eq(RecordLog::getUuid, item.getCallid()));
                if (CollUtil.isNotEmpty(recordLogList)) {
                    item.setRecordUrl(recordLogList.get(0).getFile());
                }
            }
        });
        return ResultGenerator.genSuccessResult(page);
    }
}
